package com.lazydong.apiforward.client.global;

import cn.hutool.core.util.StrUtil;
import com.lazydong.apiforward.client.gui.constant.CommonConstant;
import com.lazydong.apiforward.client.gui.pages.appmain.AppMainPageConfig;
import com.lazydong.apiforward.client.gui.pages.appmain.Environment;
import com.lazydong.apiforward.client.gui.pages.userproxy.UserProxy;
import com.lazydong.apiforward.client.gui.util.DomainUtil;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import javax.swing.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class GlobalData {
    public static final AtomicInteger LOCAL_SERVER_PORT = new AtomicInteger(443);

    public static final AtomicReference<Environment> GLOBAL_ENVIRONMENT = new AtomicReference<>();

    public static final AtomicReference<JFrame> GLOBAL_FRAME = new AtomicReference<>();

    public static final AtomicBoolean BLOCKER_STATUS = new AtomicBoolean(false);

    public static final AtomicBoolean ROUTE_SERVER_STATUS = new AtomicBoolean(false);

    public static final AtomicReference<String> CURRENT_ENVIRONMENT_DOMAIN_STR = new AtomicReference<>("");

    public static final AtomicReference<UserProxy> USER_PROXY = new AtomicReference<>();

    public static final AtomicReference<AppMainPageConfig> APP_MAIN_CONFIG = new AtomicReference<>();

    public static String getNeedProxyHosts() {
        if (!BLOCKER_STATUS.get()) {
            return null;
        }
        if (GLOBAL_ENVIRONMENT.get() == null) {
            return null;
        }
        Environment environment = GLOBAL_ENVIRONMENT.get();
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(environment.envDomain);
        UriComponents build = builder.build();
        return build.getHost();
    }

    public static String getProxyEnvDomain() {
        Environment environment = GLOBAL_ENVIRONMENT.get();
        if (environment == null) {
            return null;
        }
        String envDomain = environment.envDomain;
        if (StrUtil.isBlank(envDomain)) {
            return null;
        }
        if (!BLOCKER_STATUS.get()) {
            return envDomain;
        }

        UriComponentsBuilder envDomainBuilder = UriComponentsBuilder.fromHttpUrl(envDomain);
        UriComponents envDomainUriComponents = envDomainBuilder.build();
        String oldHost = envDomainUriComponents.getHost();
        if (oldHost != null) {
            oldHost = oldHost.replace(".", "_");
        }
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(DomainUtil.serverDomain);
        UriComponents serverDomainUriComponents = builder.build();
        int port = serverDomainUriComponents.getPort();
        StringBuilder newDomain = new StringBuilder();
        newDomain.append(serverDomainUriComponents.getScheme());
        newDomain.append("://");
        newDomain.append(CommonConstant.ApiForwardServerDomain);
        newDomain.append(":");
        newDomain.append(port);
        newDomain.append("/");
        newDomain.append(oldHost);
        System.out.println(newDomain);
        return newDomain.toString();
    }

    public static String getOpenBrowserUrl() {
        Environment environment = GLOBAL_ENVIRONMENT.get();
        if (environment == null) {
            return null;
        }
        UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(DomainUtil.serverDomain).build();
        String scheme = uriComponents.getScheme();
        if (BLOCKER_STATUS.get()) {
            return environment.envDomain;
        }
        String url = scheme + "://" + CommonConstant.ApiForwardLocalDomain;
        if (LOCAL_SERVER_PORT.get() == 443 || LOCAL_SERVER_PORT.get() == 80) {
            return url;
        }
        return url + ":" + LOCAL_SERVER_PORT.get();
    }


}
